<?php

declare(strict_types=1);

namespace App\CustHub\Mapper;

use App\CustHub\Model\ChTalkKeyword;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;

class ChTalkKeywordMapper extends AbstractMapper
{
    public function assignModel()
    {
        $this->model = ChTalkKeyword::class;
    }


    /**
     * 检查分类下是否存在关键字
     * @param mixed $cateId
     * @return bool
     */
    public function checkExistsByCateId(mixed $cateId): bool
    {
        return $this->model::query()->where('cate_id', $cateId)->exists();
    }

    public function handleSearch(Builder $query, array $params): Builder
    {

        $query->where('main_uid',$params['main_uid']);
        $query->where('uid',$params['uid']);

        if (isset($params['cate_id']) && filled($params['cate_id'])) {
            $query->where('cate_id', $params['cate_id']);
        }

        return $query;
    }

    public function handleOrder(Builder $query, ?array &$params = null): Builder
    {
        return $query->orderBy('created_at', 'desc');
    }
}